package shiyan.Javawebshiyan.shiyan2.src;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import java.io.IOException;

@WebServlet("/countServlet")
public class CountServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        // 给次数设置一个初始值 0
        int count = 0;
        // 将 0 存入到 ServletContext 中
        getServletContext().setAttribute("count", count);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 将原来的次数 count 从 ServletContext 中取出来
        int count = (int) getServletContext().getAttribute("count");
        count += 1;
        // 存入到 ServletContext 中
        getServletContext().setAttribute("count", count);
        // 向页面输出欢迎光临!
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>欢迎光临本网站! 这是您第" + count + "次访问</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}